// 文件：controllers/userController.js
const userService = require("../services/userService");
const { successResponse, errorResponse } = require("../utils/response");

// 获取所有用户
exports.getAllUsers = async (ctx) => {
  try {
    const users = await userService.getAllUsers();
    ctx.body = successResponse(users, "获取所有用户成功");
  } catch (error) {
    ctx.body = errorResponse(error);
  }
};

// 获取一个用户通过id
exports.getUserById = async (ctx) => {
  try {
    const user = await userService.getUserById(ctx.params.id);
    ctx.body = successResponse(user, "获取用户成功");
  } catch (error) {
    ctx.body = errorResponse(error);
  }
};

// 创建新用户
exports.createUser = async (ctx) => {
  try {
    const user = await userService.createUser(ctx.request.body);
    ctx.body = successResponse(user, "创建用户成功");
  } catch (error) {
    ctx.body = errorResponse(error);
  }
};

// 更新一个用户通过id
exports.updateUser = async (ctx) => {
  try {
    const user = await userService.updateUser(ctx.params.id, ctx.request.body);
    ctx.body = successResponse(user, "更新用户成功");
  } catch (error) {
    ctx.body = errorResponse(error);
  }
};

// 删除一个用户通过id
exports.deleteUser = async (ctx) => {
  try {
    await userService.deleteUser(ctx.params.id);
    ctx.body = successResponse(true, `已删除用户${ctx.params.id}`);
  } catch (error) {
    ctx.body = errorResponse(error);
  }
};
